﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;

namespace KingSite.Library.IO {
    public static class PathUtility {
        // Methods
        public static string AppendDateSuffix(string filename, DateTime date) {
            string directoryName = Path.GetDirectoryName(filename);
            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filename);
            string extension = Path.GetExtension(filename);
            return Path.Combine(directoryName, fileNameWithoutExtension + date.ToString("-yyyyMMdd") + extension);
        }

        public static string ApplicationPath() {
            if (HttpContext.Current == null) {
                return AppDomain.CurrentDomain.BaseDirectory;
            }
            return HttpRuntime.AppDomainAppPath;
        }

        public static string ChangeExtension(string filename, string newExt) {
            string directoryName = Path.GetDirectoryName(filename);
            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filename);
            if (!newExt.StartsWith(".")) {
                newExt = "." + newExt;
            }
            return Path.Combine(directoryName, fileNameWithoutExtension + newExt);
        }

        public static string CombineRelativePath(string basePath, string relativePath) {
            if (relativePath.StartsWith("/") || relativePath.StartsWith(@"\")) {
                relativePath = relativePath.Substring(1);
            }
            if (relativePath.StartsWith("~/")) {
                relativePath = relativePath.Substring(2);
            }
            relativePath = relativePath.Replace('/', '\\');
            if (!basePath.EndsWith(@"\")) {
                basePath = basePath + @"\";
            }
            return Path.Combine(basePath, relativePath);
        }

        public static void DeleteEmptyDirectories(string path) {
            if (Directory.Exists(path)) {
                string[] source = Directory.GetDirectories(path, "*.*", SearchOption.AllDirectories);
                if (source.Length != 0) {
                    foreach (string str in source.OrderByDescending<string, string>(d => d.ToLower()).ToArray<string>()) {
                        if (Directory.GetFiles(str, "*.*", SearchOption.AllDirectories).Length == 0) {
                            try {
                                Directory.Delete(str, true);
                            }
                            catch {
                            }
                        }
                    }
                }
            }
        }

        public static string GetPhysicalPath(string path) {
            if (string.IsNullOrEmpty(path)) {
                return path;
            }
            if (path.StartsWith(@"\")) {
                return path;
            }
            if (HttpContext.Current != null) {
                if (path.StartsWith("~/")) {
                    return HttpContext.Current.Server.MapPath(path);
                }
                return Path.Combine(ApplicationPath(), path);
            }
            if (path.StartsWith("~/")) {
                return Path.GetFullPath(Path.Combine(ApplicationPath(), path.Substring(2)));
            }
            return Path.GetFullPath(path);
        }

        public static void RemoveExpireFiles(string path, string pattern, bool includeSubDirs, DateTime date) {
            foreach (string str in Directory.GetFiles(path, pattern, includeSubDirs ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)) {
                try {
                    FileInfo info = new FileInfo(str);
                    if (info.LastWriteTime < date) {
                        File.Delete(str);
                    }
                }
                catch {
                }
            }
        }

    }

}
